package site.wenneng.dev.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import site.wenneng.dev.dao.DefineSprintMapper;
import site.wenneng.dev.dao.DefineSprintStoryMapper;
import site.wenneng.dev.objs.bo.StoryRemoteBO;
import site.wenneng.dev.objs.bo.TaskRemoteBO;
import site.wenneng.dev.objs.dto.StoryTaskViewDTO;
import site.wenneng.dev.objs.dto.common.Result;
import site.wenneng.dev.objs.dto.sprint.SprintStoryTaskViewDTO;
import site.wenneng.dev.objs.po.DefineSprintPO;
import site.wenneng.dev.objs.po.DefineSprintStoryPO;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class SprintService {

    private DefineSprintMapper sprintMapper;
    private DefineSprintStoryMapper sprintStoryMapper;
    private StoryService storyService;
    private TaskService taskService;

    public DefineSprintPO queryCurrentSprint() {
        LambdaQueryWrapper<DefineSprintPO> queryWrapper = Wrappers.<DefineSprintPO>lambdaQuery()
                .eq(DefineSprintPO::getCurrentFlag, 1);
        List<DefineSprintPO> sprintList = sprintMapper.selectList(queryWrapper);
        return sprintList.get(0);
    }

    public List<DefineSprintStoryPO> querySprintStory(Integer sprintId) {
        LambdaQueryWrapper<DefineSprintStoryPO> queryWrapper = Wrappers.<DefineSprintStoryPO>lambdaQuery()
                .eq(DefineSprintStoryPO::getSprintId, sprintId);
        return sprintStoryMapper.selectList(queryWrapper);
    }

    public List<SprintStoryTaskViewDTO> listStory(Integer sprintId) {
        // 冲刺包含的需求
        List<DefineSprintStoryPO> sprintStoryList = querySprintStory(sprintId);
        if (CollectionUtils.isEmpty(sprintStoryList)) {
            return Collections.emptyList();
        }
        // 需求编号
        List<String> storyNumberList = sprintStoryList.stream()
                .map(DefineSprintStoryPO::getStoryNumber).collect(Collectors.toList());
        // 查询需求
        List<StoryRemoteBO> storyList = storyService.queryStoryByNumbers(storyNumberList);
        // 查询任务
        List<TaskRemoteBO> taskList = taskService.queryByStoryNumber(storyNumberList);
        // 构建通用展示对象
        List<StoryTaskViewDTO> viewDTOList = storyService.buildViewDTO(storyList, taskList);
        // 构建冲刺展示对象
        return buildViewDTO(viewDTOList, sprintStoryList);
    }

    private List<SprintStoryTaskViewDTO> buildViewDTO(List<StoryTaskViewDTO> viewDTOList, List<DefineSprintStoryPO> sprintStoryList) {
        Map<String, DefineSprintStoryPO> map = sprintStoryList.stream()
                .collect(Collectors.toMap(DefineSprintStoryPO::getStoryNumber, item -> item, (o1, o2) -> o1));
        return viewDTOList.stream().map(sourceDTO -> {
            SprintStoryTaskViewDTO targetDTO = new SprintStoryTaskViewDTO();
            BeanUtils.copyProperties(sourceDTO, targetDTO);
            return targetDTO;
        }).collect(Collectors.toList());
    }


}
